#与非门s1=(x1*x2)横=x1横+x2横
#或门s2=x1+x2
#与或门和或门组合与门y=s1*s2=x1*x1横+x1*x2横+x2*x1横+x2*x2横=x1*x2横+x1横*x2即为异或门
import numpy as np
def AND(x1, x2):
    x=np.array([x1,x2])
    w=np.array([0.5,0.5])
    theta=0.7
    tmp =np.sum(x*w)
    if tmp <= theta:
        return 0
    elif tmp > theta:
        return 1
def NAND(x1,x2):
    x=np.array([x1,x2])
    w=np.array([-0.5,-0.5])
    tmp=np.sum(w*x)
    if tmp<=-1:
        return 0
    elif tmp>-1:
        return 1    
def OR(x1,x2):
    x=np.array([x1,x2])
    w=np.array([0.5,0.5])
    tmp=np.sum(w*x)
    if tmp<=0:
        return 0    
    elif tmp>0:
        return 1    

def XOR(x1,x2):
    s1=NAND(x1,x2)
    s2=OR(x1,x2)
    y=AND(s1,s2)
    return y
def main():
    print(XOR(0,0))
    print(XOR(1,0))
    print(XOR(0,1))
    print(XOR(1,1))
if __name__=='__main__':
    main()    